﻿@using KPIS.GERP.Models.ViewModel
@using KPIS.GERP.Models.ICM
@model IEnumerable<PurchaseOrder>

@{
    //  int pageType = ViewBag.Page;
    //  if(pageType <= 1){
    ViewBag.Title = "งานใบสั่ง/งานสัญญา [PO]";
    ViewBag.Descr = ViewBag.Title + " เพื่อทำการงานใบสั่งหรือสัญญาซื้อสั่งจ้างจากการขอซื้อ/จ้าง";

    var db = new KPIS.GERP.Models.GERPContext();
    var bq = db.BidderQuotations.ToList();
    db.Dispose();
    //  }
    //  else{
    //      ViewBag.Title = "งานสัญญาซื้อ/จ้าง";
    //      ViewBag.Descr = ViewBag.Title + " เพื่อทำสัญญาซื้อสั่งจ้างจากการขอซื้อ/จ้าง";
    //  }
}

@using (Html.BeginForm("DeleteAll", "PurchaseOrder", FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <p id="message"></p>

    <div class="row-fluid">

        <div class="block-button-menu-above">
            @*@Html.ActionLink("เพิ่ม", "Create", new { BidderId = Id }
                , new { @class="btn btn-small btn-success button-standard-width", role="button" })*@
            @Html.ButtonAdd()
            @Html.ButtonDeleteAll()
        </div>
        <div class="alert-error">@Html.ValidationSummary()</div>

        <div class="row-fluid">
            <div class="table-header span12">
                <div class="span10">
                    @ViewBag.Title
                </div>
                @Html.Partial("_ExportButton")
            </div>
        </div>

        <table id="table" class="table table-striped table-bordered table-hover" aria-describedby="table_info">
            <thead>
                <tr>
                    <th class="center" style="width:24px;">
                        <!-- Check box delete multiple records column-->
                        <label>
                            <input type="checkbox" />
                            <span class="lbl"></span>
                        </label>
                    </th>
                    <th style="width:70px;text-align: center;">
                        เลขที่
                    </th>
                    @*<th style="width:100px;text-align: center;">
                        ใบสั่ง/สัญญา
                    </th>*@
                    <th style="text-align:center;">
                        @Html.DisplayNameFor(model => model.Name)
                    </th>
                    @*<th style="width:120px;text-align: center;">
                        @Html.DisplayNameFor(model => model.PcTypeId)
                    </th>*@
                    <th style="width:120px;text-align: center;">
                        @*@Html.DisplayNameFor(model => model.OrderDate)*@
                        วันที่ทำรายการ
                    </th>
                    <th style="width:120px;text-align: center;">
                        เลขที่ซื้อจ้าง
                    </th>
                    <th style="width:120px;text-align: center;">
                        ผู้ขาย
                    </th>
                    <th style="width:120px;text-align: center;">
                        จำนวนเงิน
                    </th>
                    <th style="width:100px;text-align: center;">
                        @Html.DisplayNameFor(model => model.RecordStatus)
                    </th>
                    <th class="center" style="width:20px;">
                        <!-- Empty head column for export buttons -->&nbsp;
                    </th>
                </tr>
            </thead>

            <tbody>
                @{
                    <!--pageType -->
    //var modelCriteria = new List<PurchaseOrder>();
    //if (pageType <= 1)
    //{
    //    modelCriteria = Model.Where(p => p.Bidder.PrId != null).ToList();
    //}
    //else
    //{
    //    modelCriteria = Model.Where(p => p.Bidder.BidId != null).ToList();
    //}

    foreach (var m in (Model ?? new List<PurchaseOrder>()).OrderByDescending(o => o.PoId))
    {
        <tr>
            <td class="center">
                <!-- Check box all for delete multiple records -->
                @if (m.RecordStatus == KPIS.GERP.Models.SYS.RecordStatus.ApprovedStatus)
                {
                    <label>
                        <input type="checkbox" name="deleteIds" value="@(m.PoId)" disabled />
                        <span class="lbl"></span>
                    </label>
                }
                else
                {
                    <label>
                        <input type="checkbox" name="deleteIds" value="@(m.PoId)" />
                        <span class="lbl"></span>
                    </label>
                }

            </td>

            <td>
                @Html.ActionLink(@m.Code, "Details", new { Id = m.PoId, _ = m.Code + "-" + (m.Name ?? "").Trim().Replace(" ", "") })
            </td>
            @*<td style="text-align:center">
                @(m.IsContract ?? false ? "สัญญา" : "ใบสั่ง")
            </td>*@
            <td>
                @Html.ActionLink(@m.Name, "Details", new { Id = m.PoId, _ = m.Code + "-" + (m.Name ?? "").Trim().Replace(" ", "") })
            </td>
            @*<td style="text-align:center">
                @Html.DisplayFor(model => m.MasPurchaseType.PcTypeName)
            </td>*@
            <td style="text-align:center">
                <span style="display: none">@(m.OrderDate.HasValue ? m.OrderDate.Value.ToDbFormat() : "")</span>@Html.DisplayFor(model => m.OrderDate)
            </td>
            <td style="text-align:center">
                @Html.DisplayFor(model => m.Bidder.PurchaseRequest.Code)
            </td>
            <td style="text-align:center">
                @Html.DisplayFor(model => m.Bidder.MasContact.NameTh)
            </td>
            <td style="text-align:right">
                @((bq.Where(o => o.BidderId == m.BidderId).Sum(o => o.NetAmount) ?? 0).ToString("#,##0.00"))
            </td>
            <td style="text-align: center; font-weight: bold">
                <span class="recordstatus-@m.RecordStatus">
                    @RecordStatus.RecordStatusName(m.RecordStatus)
                </span>
            </td>
            <td class="td-actions">
                @{
        if (m.RecordStatus == KPIS.GERP.Models.SYS.RecordStatus.WaitStatus || m.RecordStatus == KPIS.GERP.Models.SYS.RecordStatus.ReturnedStatus)
        {
            @Html.Partial("_ButtonMenuEachRow", new ExportGrid
                                              {
                                                  Id = m.PoId,
                                                  DetailsUrl = Url.Action("Details") + "/" + m.PoId,
                                                  EditUrl = Url.Action("Edit") + "/" + m.PoId,
                                                  DeleteUrl = Url.Action("Delete") + "/" + m.PoId
                                              })
        }
        else
        {
            @Html.Partial("_ButtonMenuEachRow", new ExportGrid
                                        {
                                            Id = m.PoId,
                                            DetailsUrl = Url.Action("Details") + "/" + m.PoId
                                        })
        }
                }
            </td>
        </tr>
    }
                }
            </tbody>
        </table>
    </div>
}

@section scripts {

    <script type="text/javascript">
        $(function () {
            var oTable1 = $('#table').dataTable({
                "aoColumns": [
                    { "bSortable": false },
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    { "bSortable": false }
                ]
            });

            $('table th input:checkbox').on('click', function () {
                var that = this;
                $(this).closest('table').find('tr > td:first-child input:checkbox').not(":disabled")
				.each(function () {
				    this.checked = that.checked;
				    $(this).closest('tr').toggleClass('selected');
				});
            });

            $('[data-rel=tooltip]').tooltip();

            $("form").submit(function () {
                var currentForm = this;
                bootbox.confirm(function (result) {
                    var _result = result;
                    if (result) {
                        $.ajax({
                            url: "DeleteAll",
                            type: "post",
                            dataType: "json",
                            data: $(currentForm).serialize(),
                            success: function (result) {
                                if (result.success) {
                                    location.reload();
                                    $("#message").html(result.message);
                                }
                                else {
                                    $("#message").html(result.message);
                                }
                            }
                        });
                    }
                })
                return false;
            });
        })

    </script>
}